<?php
/****
操作数据库的类
****/
defined('JSJZJX')||exit('非法访问！');

class mydb {
    private static $ins = NULL;
    private $pdo = NULL;
    private $conf = array();
    
    private $insertId = 0;//添加数据的ID  
    private $affectedRows = 0;//影响行数


    protected function __construct() {
        $this->conf = loadconfig::getInstance();
        $dbname=$this->conf->db;
        $dbhost=$this->conf->host;
        $user=$this->conf->user;
        $pwd=$this->conf->pwd;
        //$driver=mysql:;
        $dbms='mysql';
        $dsn="$dbms:host=$dbhost;dbname=$dbname;charset=utf8";
        try{
            $this->pdo=new PDO($dsn,$user,$pwd);
            }catch(PDOException $e){
            echo 'Connect faild:'.$e->getMessage();
        }
    }


    public function __destruct() {
    }

    public static function getIns() {
        if(!(self::$ins instanceof self)) {
            self::$ins = new self();
        }

        return self::$ins;
    }
    

    protected function setChar($char) {
        $sql = 'set names ' . $char;
        $stmt=$this->pdo->prepare($sql);
        $rs=$stmt->execute();
        return $rs;
    }

    public function query($sql) {

        $stmt=$this->pdo->prepare($sql);
        $rs=$stmt->execute();
        $this->affectedRows=$stmt->rowCount();
        $this->insertId=$this->pdo->lastInsertId();

        log::write(date('y-m-d h:i:s a').'  '.$sql);
        //var_dump($rs);
        return $rs;
    }
    /****
     insert into tbname (username,passwd,email) values ('',)
     把所有的键名用','接起来
     implode(',',array_keys($arr));
     implode("','",array_values($arr));
    ****/
    public function autoExec($table,$arr,$mode='insert',$where = ' where 1 limit 1') {
       
        
        if(!is_array($arr)) {
            return false;
        }

        if($mode == 'update') {
            $sql = 'update ' . $table .' set ';
            foreach($arr as $k=>$v) {
                $sql .= $k . "='" . $v ."',";
            }
            $sql = rtrim($sql,',');
            $sql .= $where;
            
            return $this->query($sql);
        }

        $sql = 'insert into ' . $table . ' (' . implode(',',array_keys($arr)) . ')';
        $sql .= ' values (\'';
        $sql .= implode("','",array_values($arr));
        $sql .= '\')';

        return $this->query($sql);
    
    }

    public function getAll($sql) {
       
        $stmt=$this->pdo->prepare($sql);
        $stmt->execute();
        $rs=$stmt->fetchAll(PDO::FETCH_ASSOC);
        $list = array();
        for($i=0;$i<count($rs);$i++) {
           $list[] = $rs[$i];
        }
        log::write(date('y-m-d h:i:s a').'  '.$sql);
       return $list;
    }

    public function getAll1($sql) {
       
        $stmt=$this->pdo->prepare($sql);
        $stmt->execute();
        $rs=$stmt->fetchAll(PDO::FETCH_NUM);
        $list = array();
        for($i=0;$i<count($rs);$i++) {
           $list[] = $rs[$i];
        }
        log::write(date('y-m-d h:i:s a').'  '.$sql);
       return $list;
    }
//获取一行数据
    public function getRow($sql) {

         $stmt=$this->pdo->prepare($sql);
         $stmt->execute();
         $rs=$stmt->fetch(PDO::FETCH_ASSOC);
         log::write(date('y-m-d h:i:s a').'  '.$sql);
        return $rs;
    }
    //获取一个单个的值
    public function getOne($sql) {
        $stmt=$this->pdo->prepare($sql);
         $stmt->execute();
         $rs=$stmt->fetch(PDO::FETCH_NUM);
         log::write(date('y-m-d h:i:s a').'  '.$sql);
       
        return $rs[0];
    }

    // 返回影响行数的函数
    public function affected_rows() {
        return $this->affectedRows;
    }

    // 返回最新的auto_increment列的自增长的值
    public function insert_id() {
        return $this->insertId;
    }


}

